---
title: "maven笔记"
date: 2018-7-1
categories:
- java
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org6cd6258">Intr</a></li>
<li><a href="#org9222932">POM - Project Object Model</a></li>
<li><a href="#org2ffd43c">多模块聚合vs继承</a>
<ul>
<li><a href="#orga1e586d">继承</a></li>
</ul>
</li>
<li><a href="#org785be14">FAQ</a>
<ul>
<li><a href="#orga0c4654">创建项目</a></li>
<li><a href="#org71e49bb">引入 jar</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org6cd6258">
<h2 id="org6cd6258">Intr</h2>
<div class="outline-text-2" id="text-org6cd6258">
<blockquote>
<p>
<a href="http://maven.apache.org/pom.html">http://maven.apache.org/pom.html</a>
</p>
</blockquote>
<p>
maven - 必应词典
美['meɪvən]英['meɪvən] # 读作“没文”，不是漫威
</p>
<p>
n.专家；内行
网络火星大气与挥发物演化任务；行家；火星大气与不稳定性变化
</p>
<p>
变形复数：mavens；
例句Maven is a set of standards, a repository format, and a piece of software used to manage and describe projects.
Maven是一个标准，一个知识库格式，是一个管理和描述项目的软件。
</p>
</div>
</div>
<div class="outline-2" id="outline-container-org9222932">
<h2 id="org9222932">POM - Project Object Model</h2>
<div class="outline-text-2" id="text-org9222932">
<p>
POM.xml的直系子元素：
</p>
<div class="org-src-container">
<pre class="src src-xml">&lt;<span style="font-weight: bold;">project</span> <span style="font-weight: bold;">xmlns</span>=<span style="font-style: italic;">"http://maven.apache.org/POM/4.0.0"</span>
  <span style="font-weight: bold;">xmlns</span>:<span style="font-weight: bold; font-style: italic;">xsi</span>=<span style="font-style: italic;">"http://www.w3.org/2001/XMLSchema-instance"</span>
  <span style="font-weight: bold;">xsi</span>:<span style="font-weight: bold; font-style: italic;">schemaLocation</span>=<span style="font-style: italic;">"http://maven.apache.org/POM/4.0.0</span>
<span style="font-style: italic;">                      http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;
  &lt;<span style="font-weight: bold;">modelVersion</span>&gt;4.0.0&lt;/<span style="font-weight: bold;">modelVersion</span>&gt; <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">maven 2 &amp; 3 唯一支持的值，并且是必须的 </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>

  <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">The Basics </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
  &lt;<span style="font-weight: bold;">groupId</span>&gt;...&lt;/<span style="font-weight: bold;">groupId</span>&gt;
  &lt;<span style="font-weight: bold;">artifactId</span>&gt;...&lt;/<span style="font-weight: bold;">artifactId</span>&gt;
  &lt;<span style="font-weight: bold;">version</span>&gt;...&lt;/<span style="font-weight: bold;">version</span>&gt;
  &lt;<span style="font-weight: bold;">packaging</span>&gt;...&lt;/<span style="font-weight: bold;">packaging</span>&gt;
  &lt;<span style="font-weight: bold;">dependencies</span>&gt;...&lt;/<span style="font-weight: bold;">dependencies</span>&gt;
  &lt;<span style="font-weight: bold;">parent</span>&gt;...&lt;/<span style="font-weight: bold;">parent</span>&gt;
  &lt;<span style="font-weight: bold;">dependencyManagement</span>&gt;...&lt;/<span style="font-weight: bold;">dependencyManagement</span>&gt;
  &lt;<span style="font-weight: bold;">modules</span>&gt;...&lt;/<span style="font-weight: bold;">modules</span>&gt;
  &lt;<span style="font-weight: bold;">properties</span>&gt;...&lt;/<span style="font-weight: bold;">properties</span>&gt;

  <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">Build Settings </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
  &lt;<span style="font-weight: bold;">build</span>&gt;...&lt;/<span style="font-weight: bold;">build</span>&gt;
  &lt;<span style="font-weight: bold;">reporting</span>&gt;...&lt;/<span style="font-weight: bold;">reporting</span>&gt;

  <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">More Project Information </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
  &lt;<span style="font-weight: bold;">name</span>&gt;...&lt;/<span style="font-weight: bold;">name</span>&gt;
  &lt;<span style="font-weight: bold;">description</span>&gt;...&lt;/<span style="font-weight: bold;">description</span>&gt;
  &lt;<span style="font-weight: bold;">url</span>&gt;...&lt;/<span style="font-weight: bold;">url</span>&gt;
  &lt;<span style="font-weight: bold;">inceptionYear</span>&gt;...&lt;/<span style="font-weight: bold;">inceptionYear</span>&gt;
  &lt;<span style="font-weight: bold;">licenses</span>&gt;...&lt;/<span style="font-weight: bold;">licenses</span>&gt;
  &lt;<span style="font-weight: bold;">organization</span>&gt;...&lt;/<span style="font-weight: bold;">organization</span>&gt;
  &lt;<span style="font-weight: bold;">developers</span>&gt;...&lt;/<span style="font-weight: bold;">developers</span>&gt;
  &lt;<span style="font-weight: bold;">contributors</span>&gt;...&lt;/<span style="font-weight: bold;">contributors</span>&gt;

  <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">Environment Settings </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
  &lt;<span style="font-weight: bold;">issueManagement</span>&gt;...&lt;/<span style="font-weight: bold;">issueManagement</span>&gt;
  &lt;<span style="font-weight: bold;">ciManagement</span>&gt;...&lt;/<span style="font-weight: bold;">ciManagement</span>&gt;
  &lt;<span style="font-weight: bold;">mailingLists</span>&gt;...&lt;/<span style="font-weight: bold;">mailingLists</span>&gt;
  &lt;<span style="font-weight: bold;">scm</span>&gt;...&lt;/<span style="font-weight: bold;">scm</span>&gt;
  &lt;<span style="font-weight: bold;">prerequisites</span>&gt;...&lt;/<span style="font-weight: bold;">prerequisites</span>&gt;
  &lt;<span style="font-weight: bold;">repositories</span>&gt;...&lt;/<span style="font-weight: bold;">repositories</span>&gt;
  &lt;<span style="font-weight: bold;">pluginRepositories</span>&gt;...&lt;/<span style="font-weight: bold;">pluginRepositories</span>&gt;
  &lt;<span style="font-weight: bold;">distributionManagement</span>&gt;...&lt;/<span style="font-weight: bold;">distributionManagement</span>&gt;
  &lt;<span style="font-weight: bold;">profiles</span>&gt;...&lt;/<span style="font-weight: bold;">profiles</span>&gt;
&lt;/<span style="font-weight: bold;">project</span>&gt;
</pre>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org2ffd43c">
<h2 id="org2ffd43c">多模块聚合vs继承</h2>
<div class="outline-text-2" id="text-org2ffd43c">
<blockquote>
<p>
<a href="http://maven.apache.org/pom.html#Aggregation">http://maven.apache.org/pom.html#Aggregation</a>
</p>
</blockquote>
<div class="org-src-container">
<pre class="src src-xml">&lt;<span style="font-weight: bold;">project</span> <span style="font-weight: bold;">xmlns</span>=<span style="font-style: italic;">"http://maven.apache.org/POM/4.0.0"</span>
  <span style="font-weight: bold;">xmlns</span>:<span style="font-weight: bold; font-style: italic;">xsi</span>=<span style="font-style: italic;">"http://www.w3.org/2001/XMLSchema-instance"</span>
  <span style="font-weight: bold;">xsi</span>:<span style="font-weight: bold; font-style: italic;">schemaLocation</span>=<span style="font-style: italic;">"http://maven.apache.org/POM/4.0.0</span>
<span style="font-style: italic;">                      https://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;
  &lt;<span style="font-weight: bold;">modelVersion</span>&gt;4.0.0&lt;/<span style="font-weight: bold;">modelVersion</span>&gt;

  &lt;<span style="font-weight: bold;">groupId</span>&gt;org.codehaus.mojo&lt;/<span style="font-weight: bold;">groupId</span>&gt;
  &lt;<span style="font-weight: bold;">artifactId</span>&gt;my-parent&lt;/<span style="font-weight: bold;">artifactId</span>&gt;
  &lt;<span style="font-weight: bold;">version</span>&gt;2.0&lt;/<span style="font-weight: bold;">version</span>&gt;
  &lt;<span style="font-weight: bold;">packaging</span>&gt;pom&lt;/<span style="font-weight: bold;">packaging</span>&gt;

  &lt;<span style="font-weight: bold;">modules</span>&gt;
    &lt;<span style="font-weight: bold;">module</span>&gt;my-project&lt;/<span style="font-weight: bold;">module</span>&gt; <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">这里是路径，my-project在这个聚合模块所在的目录下 </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
    &lt;<span style="font-weight: bold;">module</span>&gt;another-project&lt;/<span style="font-weight: bold;">module</span>&gt; <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">假如这个项目在聚合模块的上级目录，则是 ../another-project </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
    &lt;<span style="font-weight: bold;">module</span>&gt;third-project/pom-example.xml&lt;/<span style="font-weight: bold;">module</span>&gt;
  &lt;/<span style="font-weight: bold;">modules</span>&gt;
&lt;/<span style="font-weight: bold;">project</span>&gt;
</pre>
</div>
<p>
一个maven模块的pom.xml文件中，它的packing=pom，并且有多个其他模块。
聚合是为了方便构建多个模块。（但是IDE都有自动构建的功能，各式插件）
</p>
<p>
当聚合这些模块时，不需要考虑它们的依赖：例如它们的顺序并不重要，maven将解决它们的依赖关系。
</p>
</div>
<div class="outline-3" id="outline-container-orga1e586d">
<h3 id="orga1e586d">继承</h3>
<div class="outline-text-3" id="text-orga1e586d">
<div class="org-src-container">
<pre class="src src-xml">&lt;<span style="font-weight: bold;">project</span>&gt;  
    &lt;<span style="font-weight: bold;">modelVersion</span>&gt;4.0.0&lt;/<span style="font-weight: bold;">modelVersion</span>&gt;  
    &lt; parent &gt;  
        <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">继承发生在这里 </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
    &lt;/ parent &gt;  
  ...  
    &lt;<span style="font-weight: bold;">modules</span>&gt;
    <span style="font-weight: bold; font-style: italic;">&lt;!-- </span><span style="font-weight: bold; font-style: italic;">继承与聚合不是同一个概念，很多情况，它们在一起用 </span><span style="font-weight: bold; font-style: italic;">--&gt;</span>
    &lt;/<span style="font-weight: bold;">moeuls</span>&gt;
&lt;/<span style="font-weight: bold;">project</span>&gt;  
</pre>
</div>
<p>
就像程序中的聚合与继承的关系一样。经常能算到有的模块既是聚合模块，也是继承。
</p>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org785be14">
<h2 id="org785be14">FAQ</h2>
<div class="outline-text-2" id="text-org785be14">
</div>
<div class="outline-3" id="outline-container-orga0c4654">
<h3 id="orga0c4654">创建项目</h3>
<div class="outline-text-3" id="text-orga0c4654">
<div class="org-src-container">
<pre class="src src-sh">mvn archetype:generate -DgroupId=xxxx -DartifactId=xxxx -Dpackag=xxx -Dversion=1.0-SNAPSHOT
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org71e49bb">
<h3 id="org71e49bb">引入 jar</h3>
<div class="outline-text-3" id="text-org71e49bb">
<div class="org-src-container">
<pre class="src src-xml">&lt;<span style="font-weight: bold;">dependency</span>&gt;
  &lt;<span style="font-weight: bold;">groupId</span>&gt;com.taobao.top&lt;/<span style="font-weight: bold;">groupId</span>&gt;
  &lt;<span style="font-weight: bold;">artifactId</span>&gt;top-api-sdk-dev&lt;/<span style="font-weight: bold;">artifactId</span>&gt;
  &lt;<span style="font-weight: bold;">version</span>&gt;dingtalk-SNAPSHOT&lt;/<span style="font-weight: bold;">version</span>&gt;
  &lt;<span style="font-weight: bold;">scope</span>&gt;system&lt;/<span style="font-weight: bold;">scope</span>&gt;
  &lt;<span style="font-weight: bold;">systemPath</span>&gt;${basedir}/../lib/taobao-sdk-java-auto_1479188381469-20190816.jar&lt;/<span style="font-weight: bold;">systemPath</span>&gt;
&lt;/<span style="font-weight: bold;">dependency</span>&gt;
</pre>
</div>
<p>
而spring-boot打包插件需要配置才能将引入的外部jar打包到部署用的jar包中：
</p>
<div class="org-src-container">
<pre class="src src-xml">&lt;<span style="font-weight: bold;">plugin</span>&gt;
  &lt;<span style="font-weight: bold;">groupId</span>&gt;org.springframework.boot&lt;/<span style="font-weight: bold;">groupId</span>&gt;
  &lt;<span style="font-weight: bold;">artifactId</span>&gt;spring-boot-maven-plugin&lt;/<span style="font-weight: bold;">artifactId</span>&gt;
  &lt;<span style="font-weight: bold;">configuration</span>&gt;
    &lt;<span style="font-weight: bold;">includeSystemScope</span>&gt;true&lt;/<span style="font-weight: bold;">includeSystemScope</span>&gt;
  &lt;/<span style="font-weight: bold;">configuration</span>&gt;
&lt;/<span style="font-weight: bold;">plugin</span>&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2018-7-1</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
